package com.example.apple.tianqiyubaoone;

import android.os.Message;
import android.util.Log;

import com.example.apple.tianqiyubaoone.DATA.Data;
import com.example.apple.tianqiyubaoone.DATA.OneDayWeatherData;
import com.example.apple.tianqiyubaoone.DATA.SK;
import com.example.apple.tianqiyubaoone.DATA.Today;
import com.example.apple.tianqiyubaoone.DATA.WeatherData;
import com.example.apple.tianqiyubaoone.DATA.Weather_id;
import com.example.apple.tianqiyubaoone.DATA.future;
import com.example.apple.tianqiyubaoone.DATA.result;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.concurrent.Future;

/**
 * Created by apple on 17/3/6.
 */

public class ParsingData {
    public static void setParsingData(Message msg){
       Data data = Data.createSingletonData();
        JSONObject js = (JSONObject) msg.obj;
        try {
            data.setResultcode(js.getString("resultcode"));
            data.setReason(js.getString("reason"));
            data.setError_code(js.getString("error_code"));
            JSONObject result = js.getJSONObject("result");

            data.setResult( parsingResult(result));
          parsingResult(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public static result parsingResult(JSONObject result){
        result result1 = new result();
        try {
            JSONObject sk = result.getJSONObject("sk");
            JSONObject today = result.getJSONObject("today");
            JSONObject future =result.getJSONObject("future");
            result1.setSk(parsingSk(sk));
            result1.setToday(parsingToday(today));
            String t =extractionOfDigital(result1.getToday().getDate_y());
            result1.setFuture(parsingFuture(future,t));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result1;
    }
    public static SK parsingSk(JSONObject sk){
        SK sk1 = new SK();
        try {
            sk1.setTime(sk.getString("time"));
            sk1.setTemp(sk.getString("temp"));
            sk1.setWind_direction(sk.getString("wind_direction"));
            sk1.setWind_strength(sk.getString("wind_strength"));
            sk1.setHumidity(sk.getString("humidity"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return sk1;
    }
    public static Today parsingToday(JSONObject today){
        Today today1 = new Today();
        WeatherData weatherData = new WeatherData();
        try {
            weatherData.setTemperature(today.getString("temperature"));
            weatherData.setWeather(today.getString("weather"));
            weatherData.setWind(today.getString("wind"));
            weatherData.setWeek(today.getString("week"));
            weatherData.setWeather_id(parsingWearherId(today.getJSONObject("weather_id")));
            today1.setWeatherData(weatherData);
            today1.setCity(today.getString("city"));
            today1.setDate_y(today.getString("date_y"));
            today1.setDressing_index(today.getString("dressing_index"));
            today1.setDressing_advice(today.getString("dressing_advice"));
            today1.setUv_index(today.getString("uv_index"));
            today1.setComfort_index(today.getString("comfort_index"));
            today1.setWash_index(today.getString("wash_index"));
            today1.setTravel_index(today.getString("travel_index"));
            today1.setExercise_index(today.getString("exercise_index"));
            today1.setDrying_index(today.getString("drying_index"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return today1;
    }

    public static Weather_id parsingWearherId(JSONObject wearherId){
        Weather_id weather_id = new Weather_id();
        try {
            weather_id.setFa(wearherId.getString("fa"));
            weather_id.setFb(wearherId.getString("fb"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return weather_id;
    }
    public static future parsingFuture(JSONObject future,String time){
        future future1 = new future();
        OneDayWeatherData oneDayWeatherData;
        int time1 =Integer.parseInt(time);
        for (int i=0;i<7;i++){
           oneDayWeatherData = new OneDayWeatherData();
            try {
                JSONObject jsonObject = future.getJSONObject("day_"+time1);
                WeatherData weatherData = new WeatherData();
                weatherData.setTemperature(jsonObject.getString("temperature"));
                Log.e("--->查符号问题", extractionOfDigital(jsonObject.getString("temperature").split("~")[0]));
                future1.getMinTemperature().add( extractionOfDigital(jsonObject.getString("temperature").split("~")[0]));
                future1.getMaxTemperature().add(extractionOfDigital(jsonObject.getString("temperature").split("~")[1]));
                weatherData.setWeather(jsonObject.getString("weather"));
                JSONObject js =jsonObject.getJSONObject("weather_id");
                Weather_id weather_id = new Weather_id();
                weather_id.setFa(js.getString("fa"));
                weather_id.setFb(js.getString("fb"));
                weatherData.setWeather_id(weather_id);
                weatherData.setWind(jsonObject.getString("wind"));
                weatherData.setWeek(jsonObject.getString("week"));
                oneDayWeatherData.setWeatherData(weatherData);
                oneDayWeatherData.setDate(jsonObject.getString("date"));
                future1.getFutureSevenDays().add(oneDayWeatherData);
                time1 = time1+1;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return future1;
    }

    public static String extractionOfDigital(String str) {
            str=str.trim();
            String str2="";
//            String s = str.substring(0,str.length()-1);
//        System.out.println("***** sub:" + s);
            if(str != null && !"".equals(str)){
                System.out.println("str:"+str);
                for(int i=0;i<str.length();i++){
                    System.out.println("char i:" + str.charAt(i));

                    if((str.charAt(i)>=48 && str.charAt(i)<=57)||str.charAt(i)==45){
                        str2+=str.charAt(i);
                    }
                }

            }
            return str2;
        }

}
